app.controller('ArrangeShiftCtrl', function ($scope, $rootScope, ajax, toaster, $modal) {

    //模糊查询
    $scope.allFlag = false;
    $scope.queryMap = {};
    $scope.timeRange = {};
    $scope.routeList = [];
    $scope.selectedItems = [];
    $scope.maxSize = $rootScope.maxSize;

    $scope.reload = $scope.page.loader('shift/bs/tpl/list');
    $scope.query = function () {
        if ($scope.routeList.selected != undefined) {
            $scope.queryMap.route__id = $scope.routeList.selected.id;
        } else if ($scope.routeList.selected == undefined) {
            delete $scope.queryMap.route__id;
        }
        $scope.reload(1, $scope.queryMap);
        if ($scope.selectedItems.length > 0) $scope.selectAll(false);
    };

    //获取线路列表
    $scope.loadRouteList = () =>
        ajax.call(new Req.GET({ resource: 'route/bs/list' }))
            .then((result) => { $scope.routeList = result.result; }, () => { });


    $scope.insert = () => {
        if ($scope.selectedItems.length == 0) {
            toaster.pop('warning', '提示', '请至少勾选一个班次档案！');
            return;
        }
        var modalInstance = $modal.open({
            templateUrl: 'template/bus/shiftTemplate/insertModal.html?v=1a1fc85330',
            controller: 'BusShiftInsertModalCtrl',
            size: 'lg',
            resolve: {
                param: () => { return { "ids": $scope.ids }; }
            }
        });
        var ids = [];
        for (var i in $scope.selectedItems) {
            ids.push($scope.selectedItems[i].id);
        }

        modalInstance.result.then(function (param) {
            var inserter = new Req.POST({
                datalist: ids,
                fields: param.data,
                resource: 'shift/bs/insert'
            });
            ajax.call(inserter).then((result) => {
                if (result.code == 200) {
                    $scope.result = result.result;
                    toaster.pop('success', '成功', '发班操作成功，请检查班次生成情况。');
                    ids = [];
                    $scope.selectAll(false);

                    $modal.open({
                        templateUrl: 'template/bus/shiftTemplate/resultList.html?v=45d1cb4378',
                        controller: 'BusResultListCtrl',
                        size: 'lg',
                        resolve: { param: () => { return { "list": $scope.result }; } }
                    });
                } else {
                    toaster.pop('error', '错误', result.result);
                }
            }, () => toaster.pop('error', '错误', "与服务器的连接出错，发班失败！"));
        });
    };

    $scope.selectAll = (state) => {
        state = state || false;
        $scope.allFlag = state;
        if (state) $scope.gridOptions.selectAll(!state);
        $scope.gridOptions.selectAll(state);
    };

    $scope.gridOptions = {
        data: 'dataList',
        enablePaging: false,
        showFooter: true,
        totalServerItems: 'dataList.length',
        selectedItems: $scope.selectedItems,
        primaryKey: 'id',
        i18n: 'zh-cn',
        enableCellEdit: true,
        columnDefs: [
            {
                field: 'batchNo',
                width: '*',
                displayName: '批次号',
                cellFilter: '',
                cellTemplate: '',
                resizable: false,
                pinnable: false,
                sortable: true,
                visible: true,
                enableCellEdit: true
            },
            {
                field: 'shiftNo',
                width: '*',
                displayName: '班次号',
                cellFilter: '',
                cellTemplate: '',
                resizable: false,
                pinnable: false,
                sortable: true,
                visible: true,
                enableCellEdit: true
            },
            {
                field: 'status',
                width: '*',
                displayName: '状态',
                cellFilter: '',
                cellTemplate: `<span ng-if="COL_FIELD==1">正常</span>
                    <span ng-if="COL_FIELD==2">暂停</span>
                    <span ng-if="COL_FIELD==3">停用</span>1`,
                resizable: false,
                pinnable: false,
                sortable: true,
                visible: true,
                enableCellEdit: false
            },
            {
                field: 'route.name',
                width: '*',
                displayName: '线路名',
                cellFilter: '',
                cellTemplate: '',
                resizable: false,
                pinnable: false,
                sortable: true,
                visible: true,
                enableCellEdit: false
            },
            {
                field: 'startTime',
                width: '*',
                displayName: '开班时间',
                cellFilter: 'date: "HH:mm:ss"',
                cellTemplate: '',
                resizable: false,
                pinnable: false,
                sortable: true,
                visible: true,
                enableCellEdit: false
            },
            {
                field: 'intervalDay',
                width: '*',
                displayName: '间隔天数',
                cellFilter: '',
                cellTemplate: '<div class="ui-grid-cell-contents">{{COL_FIELD}}天</div>',
                resizable: false,
                pinnable: false,
                sortable: true,
                visible: true,
                enableCellEdit: false
            },
            {
                field: 'tickets',
                width: '*',
                displayName: '车票数',
                cellFilter: '',
                cellTemplate: '<div class="ui-grid-cell-contents">{{COL_FIELD}}张</div>',
                resizable: false,
                pinnable: false,
                sortable: true,
                visible: true,
                enableCellEdit: false
            },
        ]
    };

    setTimeout(() => $scope.loadRouteList(), 200);
    $rootScope.maxSize = 100;
    setTimeout(() => $scope.query(), 30);
});